home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / ln03 / rose / specials.c < prev    next >
C/C++ Source or Header  |  1990-10-01  |  12KB  |  481 lines

  1. /* This file contains the code that implements the handling of \special's
  2. in Dvi2ln3. 
  3.  
  4. Copyright (c) 1985 by Digital Equipment Corporation. Author: Flavio Rose,
  5. (617) 467-7545, ...ucbvax!decwrl!dec-rhea!dec-dvinci!rose. 
  6.  
  7. Right now, the following \special's are implemented: 
  8.  
  9. ln03:defpoint fixnum ( [dimension] , [dimension] )
  10. ln03:connect fixnum [/ fixnum] fixnum [/ fixnum]
  11. ln03:plotfile filename
  12. ln03:resetpoints fixnum fixnum
  13.  
  14. A dimension is a number with optional decimal point, followed by one of pt,
  15. in, pc, cm, mm, bp, dd, cc, and mi. 
  16.  
  17. We try to mimic the functionality of DVIAPS, a program that drives the
  18. Autologic Micro-5 typesetter, written by Textset Inc. of Ann Arbor,
  19. Michigan. This has been done on the basis of user-level documentation
  20. supplied by Textset. This code does not contain any Textset-proprietary
  21. information. 
  22.  
  23. \special's are parsed case-insensitively.
  24.  
  25. Revision history:
  26.  
  27. 10/8/85        Created file, wrote, tested code for ln03:plotfile
  28.  
  29. 10/16/85    ln03:resetpoints, ln03:defpoint, ln03:connect
  30.  
  31. 12/12/85    Corrected bug: ln03:plotfile does not update LN03's
  32.             position to the latest from the DVI file.
  33.  
  34. */
  35.  
  36. #ifdef vms
  37. #include stdio
  38. #include ctype
  39. #include math
  40. #include file
  41. #else
  42. #include <stdio.h>
  43. #include <ctype.h>
  44. #include <math.h>
  45. #endif
  46.  
  47. /* In VMS we declare external variables to be globalref. 
  48. This is not really necessary, just an old habit. */
  49.  
  50. #ifdef vms
  51. #define GLOBAL globaldef
  52. #define EXTERN globalref
  53. #else
  54. #define GLOBAL 
  55. #define EXTERN extern
  56. #endif
  57.  
  58. #define NUMSPECIALS 4
  59.  
  60. GLOBAL char *specialnames[NUMSPECIALS] = { "defpoint",
  61.     "connect", "plotfile", "resetpoints" } ;
  62.  
  63. EXTERN FILE *dvifile,*outfile;
  64.  
  65. /* There are two top-level routines for handling specials, one to be
  66. invoked during pass1, the other to be invoked during pass2. The parameter
  67. that is passed is the size of the \special string. */ 
  68.  
  69. int do_special_pass1 (p) 
  70. int p;
  71. {
  72.     for (; p != 0; p--) getc(dvifile);
  73.     return(0);
  74. }
  75.  
  76. /* Specials get put in a buffer for easier parsing. The buffer is a little
  77. longer than needed to make some things simpler. [[This ought to be
  78. cleaner.]] */ 
  79.  
  80. #define MAXSPECIAL 1000
  81.  
  82. GLOBAL char sb[MAXSPECIAL+7];
  83. GLOBAL int sbp,cstart;
  84.  
  85. /* Do_special_pass2 gets called from the main program to perform special
  86. processing in pass2, the pass in which stuff actually gets written out to
  87. the LN3 output file. The parameter p is the length of the special. */
  88.  
  89. int do_special_pass2 (p)
  90. int p;
  91. {
  92.     char c;
  93.     int i,j;
  94.  
  95. /* Skip whitespace in input */
  96.         
  97.     sbp = 0;
  98.     while (p != 0) {
  99.         c = getc(dvifile);
  100.         p--;
  101.         if (!isspace(c)) {
  102.         sb[sbp] = c;
  103.         sbp = 1;
  104.         break;
  105.     }
  106.     }
  107.     if (sbp == 0) return(1);
  108.  
  109. /* Now we have non-whitespace, read the rest of the special, or as much as
  110. will fit in the buffer. Lowercase the stuff as it is read in. */ 
  111.  
  112.     for (; p != 0; p--) {
  113.         sb[sbp] = tolower(getc(dvifile));
  114.         sbp++;
  115.         if (sbp > MAXSPECIAL) break;
  116.     }
  117.  
  118. /* Put a null as sentinel at the end of the special. */
  119.  
  120.     sb[sbp] = 0;
  121.  
  122. /* Determine whether the special pertains to the ln03, in which case sbpf
  123. will contain "ln03" followed by whitespace or a colon. */ 
  124.  
  125.     if (strncmp(sb,"ln03",sizeof("ln03")-1) != 0) return(1);
  126.     sbp = sizeof("ln03")-1;
  127.  
  128. /* Skip whitespace and one colon */
  129.  
  130.     while (isspace(sb[sbp])) sbp++;
  131.     if (sb[sbp] != ':') return(1);
  132.     sbp++;
  133.  
  134. /* Now, it might be that the special was too long for the special buffer,
  135. so check that out and issue an error message. */ 
  136.  
  137.     if (p != 0) {
  138.         printf("\n \special too long (over %d bytes).",MAXSPECIAL);
  139.         for (; p != 0; p--) getc(dvifile);
  140.         return(1);
  141.     }
  142.  
  143. /* Now determine if the special command is one of those which the driver is
  144. supposed to recognize. As in the DVIAPS program, only the first six bytes
  145. of the command are significant, so plotfile could be written plotfi and
  146. defpoint could be written defpoi. */ 
  147.  
  148.     while (isspace(sb[sbp])) sbp++;
  149.     cstart = sbp;
  150.     while (!isspace(sb[sbp]) && sb[sbp] != '\0') sbp++;
  151.     sb[sbp] = 0;
  152.     sbp++;
  153.  
  154.     for (i = 0; i<NUMSPECIALS; i++)
  155.         if (strncmp(&sb[cstart],specialnames[i],6) == 0) break;
  156.  
  157.     if (i == NUMSPECIALS) {
  158.         printf("\n Unrecognized option '%s' ignored in \special command",
  159.             &sb[cstart]);
  160.         return(1);
  161.     } else {
  162.         switch (i) {
  163.         case 0:
  164.         j = do_defpoint_pass2();
  165.             break;
  166.         case 1:
  167.             j = do_connect_pass2();
  168.             break;
  169.         case 2:
  170.             j = do_plotfile_pass2();
  171.         case 3:
  172.             j = do_resetpoints();
  173.         }
  174.     }
  175. }
  176.  
  177. /* In certain \specials, dimensions may be specified in any of nine
  178. different units of measure. */ 
  179.  
  180. #define MAXUNITS 9
  181.  
  182. GLOBAL char *unit_array[MAXUNITS] = { "pt", "in", "pc", "cm", "mm", "bp", 
  183.     "dd", "cc", "mi" };
  184.  
  185. /* Unit_convert gives a floating point conversion factor from units to
  186. pixels. [[The factor for micas is doubtful.]] */ 
  187.  
  188. #define PIXELS_PER_INCH (300.0)
  189.  
  190. GLOBAL float unit_convert[MAXUNITS] = { PIXELS_PER_INCH/72.27, 
  191.     PIXELS_PER_INCH, 12.0*PIXELS_PER_INCH/72.27,
  192.     PIXELS_PER_INCH/2.54, PIXELS_PER_INCH/25.4, 
  193.     PIXELS_PER_INCH/72.0, (1238.0/1157.0)*PIXELS_PER_INCH/72.27,
  194.     12.0*(1238.0/1157.0)*PIXELS_PER_INCH/72.27, PIXELS_PER_INCH/2540.0 };
  195.  
  196. /* There are 255 point variables, numbered 1 through 255. */
  197.  
  198. #define MAXPOINTS 255
  199.  
  200. GLOBAL int point_hh[MAXPOINTS], point_vv[MAXPOINTS];
  201. GLOBAL int point_present[MAXPOINTS];
  202.  
  203. GLOBAL char *special_mess = "\n Error in \special{ln03:%s...} parameters";
  204.  
  205. /* For the purposes of defpoint, we need to know the current horizontal and
  206. vertical positions in pixels. */ 
  207.  
  208. EXTERN int hh,vv,hoff,voff;
  209.  
  210. /* ln03:defpoint fixnum ( [dimension] , [dimension] ) */
  211.  
  212. int do_defpoint_pass2()
  213. {
  214.     int i,which,hh_val,vv_val;
  215.     float xx_p,yy_p;
  216.  
  217.     which = 0;
  218.     if (scan_fixnum(&which) != 0 || which < 1 || which > MAXPOINTS) {
  219.         printf(special_mess,"defpoint");
  220.         printf("\n Invalid point number (%d)",which);
  221.         return(1);
  222.     }
  223.     while (isspace(sb[sbp]) || sb[sbp] == ',') sbp++;
  224.     hh_val = hh;
  225.     vv_val = vv;
  226.     if (sb[sbp] != '(') goto record_point;
  227.     sbp++;
  228.     while (isspace(sb[sbp])) sbp++;
  229.     if (sb[sbp] == '\0') goto record_point;
  230.     if (sb[sbp] == ',') sbp++;
  231.     else {
  232.         scan_dimension(&hh_val);
  233.         while (isspace(sb[sbp])) sbp++;
  234.         if (sb[sbp] == ',') sbp++;
  235.         else goto record_point;
  236.     } 
  237.     while(isspace(sb[sbp])) sbp++;
  238.     if (sb[sbp] != ')' && sb[sbp] != '\0') scan_dimension(&vv_val);
  239. record_point:
  240.     point_hh[which-1] = hh_val;
  241.     point_vv[which-1] = vv_val;
  242.     point_present[which-1] = 1;
  243.     return(0);
  244. }
  245.  
  246. EXTERN long first_counter;
  247.  
  248. int do_connect_pass2()
  249. {
  250.     int a1,b1,a2,b2,w;
  251.     
  252.     scan_xpoint(&a1,&b1);
  253.     while (isspace(sb[sbp]) || sb[sbp] == ',') sbp++;
  254.     scan_xpoint(&a2,&b2);
  255.     while (isspace(sb[sbp]) || sb[sbp] == ',') sbp++;
  256.     w = 2; /* default width is two pixels ~ 0.4 points */
  257.     if (sb[sbp] != '\0') scan_dimension(&w);
  258.  
  259.     if (first_counter%2 == 0) connect_points(b1,b2,w);
  260.     else connect_points(a1,a2,w);
  261.  
  262.     return(0);
  263. }
  264.  
  265. EXTERN int ln3p,vpset,hh_old;
  266.  
  267. int do_plotfile_pass2()
  268. {
  269.     char buf[512];
  270.     int i,j;
  271.     char c;
  272.  
  273. /* Read filename into array. */ 
  274.  
  275.     while (isspace(sb[sbp])) sbp++;
  276.     i = sbp;
  277.     while (!isspace(sb[sbp]) && sb[sbp] != '\0') sbp++;
  278.     sb[sbp] = 0;
  279.  
  280. /* Try to open the file. [[What is the status of O_RDONLY under Unix? Is
  281. that a 4.2bsd hack that it would be best to leave out?]] */ 
  282.  
  283. #ifdef vms
  284.     j = open(&sb[i],O_RDONLY,0);
  285. #else
  286.     j = open(&sb[i],0,0);
  287. #endif
  288.     if (j == -1) {
  289.         printf("\n Unable to open plotfile %s",&sb[i]);
  290.         return(1);
  291.     }
  292.  
  293. /* In executing the plotfile special, always emit escape sequences to place
  294. the LN03 at the current position of the DVI file, even if it would seem
  295. that the LN03 is already there. We do this in order to be able to emit a
  296. newline at this point. Emitting a newline at this point keeps the line
  297. length of the .ln3 file to <= 16 characters more than the line length of
  298. the plotfile. */ 
  299.  
  300.     fprintf(outfile,"\n\033[%dd\033[%d`",vv+voff,hh+hoff);
  301.  
  302. /* Read stuff from the plotfile, write it to the output file. [[Could this
  303. cause problem with one record being split into two?]] */ 
  304.  
  305.     while((i = read(j,buf,512)) > 0) 
  306.         fwrite(buf,i,1,outfile);
  307.     close(j);
  308.  
  309. /* Now, set the variables ln3p, vpset and hh_old to indicate to the caller
  310. that its recorded value of the current position of the LN03 is no longer
  311. correct. This will make the caller issue absolute positioning commands
  312. before doing any further output to the dvifile. */ 
  313.  
  314.     ln3p = 0;
  315.     vpset = 0;
  316.     hh_old = 30000;
  317.     return(0);
  318. }
  319.  
  320.  
  321. /* Scan_fixnum reads an integer off sb[], starting at sbp and advancing sbp
  322. to the first character past a valid integer. The integer is returned in i.
  323. */ 
  324.  
  325. int scan_fixnum(i)
  326. long *i;
  327. {
  328.     int sbp_save;
  329.  
  330.     sbp_save = sbp;
  331.     *i = atol(&sb[sbp]);
  332.     while(isspace(sb[sbp])) sbp++;
  333.     if (sb[sbp] == '+' || sb[sbp] == '-') sbp++;
  334.     while (isdigit(sb[sbp])) sbp++;
  335.     return(sbp == sbp_save);
  336. }
  337.  
  338.  
  339. /* Scan_flonum reads a flonum (without exponential notation) off sb[],
  340. starting at sbp and advancing sbp to the first character past a valid
  341. flonum. The flonum is returned in x. [[Unfortunately, it is impossible to
  342. use atof or sscanf to implement this function, because they don't allow
  343. flonums like ".3" which don't have any digits before the decimal point.]]
  344. */ 
  345.  
  346. int scan_flonum(x)
  347. float *x;
  348. {
  349.     float j,frac;
  350.     char negative;
  351.     int sbp_save;
  352.  
  353.     sbp_save = sbp;
  354.     negative = 0;
  355.     j = 0.0;
  356.     while (isspace(sb[sbp])) sbp++;
  357.     if (sb[sbp] == '-') { negative = 1; sbp++; }
  358.     else if (sb[sbp] == '+') sbp++;
  359.     while (isdigit(sb[sbp])) { 
  360.         j = 10.0*j + ((float)(sb[sbp] - '0'));
  361.         sbp++;
  362.     }
  363.     if (sb[sbp] == '.') {
  364.         sbp++;
  365.         frac = 0.1;
  366.         while (isdigit(sb[sbp])) {
  367.         j += frac*((float)(sb[sbp] - '0'));
  368.         frac *= 0.1;
  369.         sbp++;
  370.     }
  371.     }
  372.     if (sbp != sbp_save) {
  373.         *x = negative ? (-j) : j;
  374.         return(0);
  375.     } else return(1);
  376.     
  377. }
  378.  
  379.  
  380. /* Scan_dimension tries to parse a dimension off sb[], advancing sbp over
  381. what it can parse. The dimension is converted to pixels and returned in
  382. val. If the unit isn't recognizable, scan_dimension prints an error
  383. message, returns 1 and leaves val unchanged. */ 
  384.  
  385. int scan_dimension(val)
  386. int *val;
  387. {
  388.     float x;
  389.     int i;
  390.  
  391.     x = 0.0;
  392.     scan_flonum(&x);
  393.     while (isspace(sb[sbp])) sbp++;
  394.     for (i = 0; i<MAXUNITS; i++)
  395.         if (strncmp(&sb[sbp],unit_array[i],2) == 0) break;
  396.     if (i == MAXUNITS) {
  397.         printf(special_mess,&sb[cstart]);
  398.         printf("\n No such unit of measure: %.2s",&sb[sbp]);
  399.         return(1);
  400.     }
  401.     sbp += 2;
  402.     x *= unit_convert[i];
  403. #ifdef vms
  404.     *val = (x < 0) ? floor(x-0.5) : floor(x+0.5);
  405. #else
  406.     *val = (x < 0) ? (x-0.5) : (x+0.5);
  407. #endif
  408.     return(0);
  409. }
  410.  
  411. /* Scan_xpoint picks an xpoint off sb[], advancing sbp as usual. If both
  412. parts are present they are returned in a,b; if only one is, it's returned
  413. in both a and b. */ 
  414.  
  415. int scan_xpoint(a,b)
  416. int *a,*b;
  417. {
  418.  
  419.     *a = 0; *b = 0;
  420.     scan_fixnum(a);
  421.     while (isspace(sb[sbp])) sbp++;
  422.     if (sb[sbp] == '/') {
  423.         sbp++;
  424.         scan_fixnum(b);
  425.     } else *b = *a;
  426.     return(0);
  427. }
  428.  
  429. /* int do_rule(xx0,yy0,xx1,yy1) */
  430.  
  431. int connect_points(b1,b2,w) 
  432. int b1,b2;
  433. int w;
  434. {
  435.     int halfw;
  436.  
  437.  
  438.     if (b1 < 1 || b1 > MAXPOINTS || !point_present[b1-1]) {
  439.         printf(special_mess,&sb[cstart]);
  440.         printf("\n Invalid point number (%d)",b1);
  441.         return(1);
  442.     }
  443.     if (b2 < 1 || b2 > MAXPOINTS || !point_present[b2-1]) {
  444.         printf(special_mess,&sb[cstart]);
  445.         printf("\n Invalid point number (%d)",b2);
  446.         return(1);
  447.     }
  448.  
  449.     halfw = w/2;
  450.     if (point_hh[b1-1] == point_hh[b2-1]) 
  451.         do_rule(point_hh[b1-1]-halfw,
  452.             point_vv[b1-1],point_hh[b1-1]+w-1-halfw,
  453.             point_vv[b2-1]);
  454.     else if (point_vv[b1-1] == point_vv[b2-1])
  455.         do_rule(point_hh[b1-1],
  456.             point_vv[b1-1]-halfw,point_hh[b2-1],
  457.             point_vv[b1-1]+w-1-halfw);
  458.     else {
  459.         printf(special_mess,&sb[cstart]);
  460.         printf("\n Can't connect along a diagonal.");
  461.         return(1);
  462.     }        
  463.     return(0);
  464. }
  465.  
  466. int do_resetpoints ()
  467. {
  468.     int a,b;
  469.  
  470.     a = 0;
  471.     scan_fixnum(&a);
  472.     b = 0;
  473.     scan_fixnum(&b);
  474.  
  475.     if (0 < a && a <= b && b <= MAXPOINTS) 
  476.         for (; a <= b; a++) point_present[a] = 0;
  477.     else if (0 < a && a <= MAXPOINTS)
  478.         point_present[a] = 0;
  479.     return(0);
  480. }
  481.